
import { BuiltInData } from '../../Config/tableOptions'
import { BaseFormButton } from '../../Utils/TableButton'
import { commonTableList } from '../../Config/tableCommon'
import { getProjectMenuOldTree } from '../../Api/tableList'

// TODO 入参 uuid, url, ProTableEvent, ClientData, Permission, actionRef
const ProjectMenu = async ({ url, ProTableEvent, actionRef }) => {
    const get = `get${url}OldTree`
    const pos = `posCommon${url}`
    const put = `putCommon${url}`
    const del = `delCommon${url}`
    const initialValues = { isEnabled: 1 }

    const options = () => {
        return ([
            <BaseFormButton
                key='add'
                event='NewAdd'
                ProTableEvent={ProTableEvent}
                type="primary"
                icon="add"
                title="新增"
                data={{ params: { pos, put, initialValues, columns } }}
            />
        ])
    }

    const columns = [
        commonTableList.uuid,
        { ...commonTableList.tableName, title: '菜单名称', width: 300 },
        { ...commonTableList.sortId, rules: true },
        {
            ...commonTableList.parentUuid,
            fieldProps: {
                fieldNames: { label: 'tableName', value: 'uuid' },
                options: await getProjectMenuOldTree(),
            },
        },
        {
            dataIndex: 'type',
            title: '类型',
            width: 100,
            rules: true,
            valueType: 'select',
            valueEnum: BuiltInData.MenuType,
            hideInSearch: true,
            enumObject: true,
        },
        {
            dataIndex: 'targetUrl',
            title: '页面地址',
            valueType: 'input',
            rules: true,
            width: 200,
            valueJudge: 'type',
            judgeValue: '0',
            hideInTable: true,
        },
        {
            dataIndex: 'pageType',
            title: '页面类型',
            valueType: 'select',
            valueEnum: BuiltInData.PageType,
            rules: true,
            valueJudge: 'type',
            judgeValue: '0',
            hideInTable: true,
        },
        { ...commonTableList.isEnabled, rules: true },
        {
            dataIndex: 'icon',
            title: 'icon',
            valueType: 'icon',
            valueJudge: 'parentUuid',
            judgeValue: '00000000-0000-0000-0000-000000000000',
            rules: true,
            hideInSearch: true,
            hideInTable: true,
        },
        commonTableList.remark,
        {
            title: '操作',
            valueType: 'systemOperation',
            fixed: 'right',
            width: 240,
            hideInSearch: true,
            hideInAdded: true,
            hideInModify: true,
            render: (_, record) => {
                const { uuid, targetUrl, children, type } = record
                return [
                    targetUrl !== 'Home' && type !== 1 && (
                        <BaseFormButton
                            key='add'
                            event='NewAdd'
                            ProTableEvent={ProTableEvent}
                            title="追加"
                            data={{ params: { pos, put, initialValues, columns }, addTo: { parentUuid: uuid } }}
                        />
                    ),
                    <BaseFormButton
                        key='A01'
                        event='Modify'
                        ProTableEvent={ProTableEvent}
                        title="修改"
                        data={{ params: { pos, put, initialValues, columns }, record }}
                    />,
                    targetUrl !== 'Home' && !children &&
                    <BaseFormButton
                        key='delete'
                        event='Delete'
                        ProTableEvent={ProTableEvent}
                        title="删除"
                        danger
                        data={{ url: del, path: [uuid], actionRef }}
                    />,
                ]
            },
        },
    ]




    return {
        get,
        columns,
        hideInSearch: true,
        params: { pageIndex: 1, pageSize: 1000 },
        options
    }
}

export default ProjectMenu